var window_write_orgcheck = new Ext.Window({
	title : '考核指标填报',
	width : 480,
	height : 640,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('考核指标填报');
			Ext.getCmp("form_write_orgcheck").getForm().reset();
		}
	},
	items : [new Ext.FormPanel({
				id : 'form_write_orgcheck',
				labelWidth : 120,
				labelAlign : 'right',
				url : '/supervise/biz/orgcheck/writeOrgCheck.action',
				method : 'post',
				border : false,
				baseCls : 'x-plain',
				bodyStyle : 'padding:5px 5px 0',
				defaults : {
					anchor : '90%',
					width : 240,
					msgTarget : 'side' // 验证信息显示右边
				},
				defaultType : 'textfield',
				items : [{
						xtype : 'hidden',
						name : 'orgCheck.id'
					},{
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>指标类型',
						hiddenName  : 'orgCheck.type',
						allowBlank : false,
						readOnly : true,
						editable : false,
						disabled : true,
						store : new Ext.data.SimpleStore({
									data : data_goal_type,
									fields : ['text', 'value']
								}),
						displayField : 'text',
						valueField : 'value',
						mode : 'local',
						value: 'normal',
						triggerAction : 'all',
						emptyText : '请选择类型',
						listeners : {
//							'select' : function(combo, record, index) {
//								var typeValue =  combo.getValue();
//								if(Ext.isEmpty(typeValue)){
//									typeValue = 'normal';
//								} 
//							try {
//								var detailId = Ext.getCmp('edit_orgCheck_detailId');
//								detailId.clearValue();
//								detailId.store.reload({
//											params : {
//												type : typeValue
//											}
//										});
//							} catch (ex) {
//								Ext.MessageBox.alert(ex);
//							}
//						
//							}
						}
					}, {
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>指标细则',
						id: 'edit_orgCheck_detailId',
						hiddenName  : 'orgCheck.detailId',
						valueField : 'id',
						displayField : 'name',
						mode : 'local',
						store : new Ext.data.Store({
							url : '/supervise/biz/orgcheck/findGoalByType.action',
							autoLoad : true,
							reader : new Ext.data.JsonReader({
									root : 'root'
								}, [{
										name : 'id',
										type : 'int'
									}, {
										name : 'name',
										type : 'string'
									}])
						}),
						selectOnFocus : true,
						editable : false,
						allowBlank : false,
						disabled : true,
						triggerAction : 'all',
						loadingText : '加载中...',
						emptyText : '请选择指标细则'
					}, {
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>被考核部门',
						hiddenName : 'orgCheck.orgId',
						valueField : 'id',
						displayField : 'name',
						mode : 'remote',
						store : ds_org_select,
						selectOnFocus : true,
						editable : false,
						allowBlank : false,
						disabled : true,
						triggerAction : 'all',
						loadingText : '加载中...',
						emptyText : '请选择部门',
						listeners : {
//							'select' : function(combo, record, index) {
//								var frm = Ext.getCmp("form_add_orgcheck").getForm();
//								frm.findField('orgCheck.orgId').setValue(record.data.id);
//							}
						}
					}, {
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>指标的进展状态',
						name : 'orgCheck.goStatus',
						triggerAction : 'all',
						loadingText : '加载中...',
						emptyText : '请选择进展状态',
						mode : 'local',
						valueField : 'value',
						displayField : 'value',
						store : new  Ext.data.SimpleStore({
							data: data_go_status,
							expandData: true,
							fields : ['value']
						}),
						allowBlank : false
					}, {
						xtype : 'textarea',
						fieldLabel : '<span style="color: red">*&nbsp;</span>指标的完成情况',
						name : 'orgCheck.finishResult',
						allowBlank : false,
						maxLength : 20
					}, {
						xtype : 'hidden',
						fieldLabel : '考核得分',
						name : 'orgCheck.score'
					}, {
						xtype : 'numberfield',
						fieldLabel : '<span style="color: red">*&nbsp;</span>自评分',
						name : 'orgCheck.selfScore',
						allowBlank : false
					},{
						xtype : 'numberfield',
						fieldLabel : '指标标准分',
						name : 'orgCheck.point',
						allowBlank : false
					},  {
						fieldLabel : '备注',
						name : 'orgCheck.memo',
						maxLength : 200
					}, {
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>考核责任单位',
						hiddenName : 'orgCheck.checkOrgId',
						valueField : 'id',
						displayField : 'name',
						mode : 'remote',
						store : ds_org_select,
						selectOnFocus : true,
						editable : false,
						allowBlank : false,
						disabled : true,
						triggerAction : 'all',
						loadingText : '加载中...',
						emptyText : '请选择部门',
						listeners : {
							'select' : function(combo, record, index) {
								var frm = Ext.getCmp("form_add_orgcheck").getForm();
								frm.findField('orgCheck.checkOrgId').setValue(record.data.id);
							}
						}
					}, {
						xtype : 'hidden',
						fieldLabel : '考核意见',
						name : 'orgCheck.checkResult',
						allowBlank : true
					}, {
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
						hiddenName : 'orgCheck.year',
						mode : 'local',
						store : store_year,
						valueField : 'value',
						displayField : 'text',
						value: check_year,
						selectOnFocus : true,
						editable : false,
						allowBlank : false,
						readOnly : true,
						disabled : true,
						triggerAction : 'all',
						emptyText : '请选择考核年度'
					}],
				buttonAlign : 'center',
				minButtonWidth : 60,
				buttons : [{
							text : '提交',
							handler : function(btn) {
								var frm = Ext.getCmp("form_write_orgcheck").getForm();
								var selfScore = frm.findField('orgCheck.selfScore').getValue();
								var point = frm.findField('orgCheck.point').getValue();
								if(Ext.num(selfScore, 0) > Ext.num(point, 0)){
									Ext.MessageBox.alert("提示", "自评分不能大于标准分！");
									return false;
								}
								if (frm.isValid()) {
									btn.disable();
									var cnfield = frm.findField('orgCheck.content');
									frm.submit({
												waitTitle : '请稍候',
												waitMsg : '正在提交表单数据,请稍候...',
												success : function(form, action) {
													var record = grid_orgcheckself.getSelectionModel().getSelected();
													// set value
													record.set("detailId", frm.findField('orgCheck.detailId').getValue());
													record.set("orgId", frm.findField('orgCheck.orgId').getValue());
													record.set("goStatus", frm.findField('orgCheck.goStatus').getValue());
													record.set("finishResult", frm.findField('orgCheck.finishResult').getValue());
//													record.set("point", frm.findField('orgCheck.poing').getValue());
													record.set("score", frm.findField('orgCheck.score').getValue());
													record.set("selfScore", frm.findField('orgCheck.selfScore').getValue());
													record.set("memo", frm.findField('orgCheck.memo').getValue());
													record.set("checkOrgId", frm.findField('orgCheck.checkOrgId').getValue());
													record.set("checkResult", frm.findField('orgCheck.checkResult').getValue());
													record.set("year", frm.findField('orgCheck.year').getValue());
													record.set("type", frm.findField('orgCheck.type').getValue());
													record.commit();
													frm.reset();
													btn.enable();
													window_write_orgcheck.hide();
												},
												failure : function() {
													Ext.Msg.show({
																title : '错误提示',
																msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																buttons : Ext.Msg.OK,
																fn : function() {
																	cnfield.focus(true);
																	btn.enable();
																},
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							}
						}, {
							text : '取消',
							handler : function() {
								window_write_orgcheck.hide();
							}
						}]
			})]
});

var cm_orgcheckself = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
    {id: 'year',header: "考核年度", width: 120,hidden: true, dataIndex: 'year'},
    {id: 'type',header: "指标类型", width: 80, sortable: true, dataIndex: 'type', renderer : fun_getGoalType},
	{id: 'detailId',header: "指标细则", width: 100, hidden: true, dataIndex: 'detailId'},
	{id: 'content',header: "指标细则", width: 100, sortable: true, dataIndex: 'content'},
	{id: 'orgId',header: "被考核部门", width: 100, hidden: true, dataIndex: 'orgId',
		renderer : function(v) {
			var ret = '';
			var size = ds_org_select.getCount();
			for (var i = 0; i < size; i++) {
				var record = ds_org_select.getAt(i);
				var id = record.get('id');
				if (id == v) {
					ret = record.get('name');
					break;
				}
			}
			return (ret == '' || ret == null) ? v : ret;
		}},
	{id: 'memo',header: "备注", width: 100, hidden: true, dataIndex: 'memo'},
	{id: 'checkOrgId',header: "考核责任单位", width: 100, sortable: true, dataIndex: 'checkOrgId',
		renderer : function(v) {
			var ret = '';
			var size = ds_org_select.getCount();
			for (var i = 0; i < size; i++) {
				var record = ds_org_select.getAt(i);
				var id = record.get('id');
				if (id == v) {
					ret = record.get('name');
					break;
				}
			}
			return (ret == '' || ret == null) ? v : ret;
		}},
	{id: 'checkResult',header: "考核意见", width: 100, hidden: true, dataIndex: 'checkResult'},
	{id: 'goStatus',header: "进展状态", width: 150, sortable: true, dataIndex: 'goStatus',
		editor : new Ext.form.ComboBox({
			fieldLabel : '进展状态',
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择进展状态',
			mode : 'local',
			valueField : 'value',
			displayField : 'value',
			store : new  Ext.data.SimpleStore({
				data: data_go_status,
				expandData: true,
				fields : ['value']
			}),
			allowBlank : true
		})},
	{id: 'finishResult',header: "完成情况", width: 160, sortable: true, dataIndex: 'finishResult',
		editor : new Ext.form.TextField({
			allowBlank : true,
			maxLength : 100
		})},
	{id: 'point',header: "指标标准分", width: 100, sortable: true, dataIndex: 'point'},
	{id: 'selfScore',header: "自评分", width: 100, sortable: true, dataIndex: 'selfScore',
		editor : new Ext.form.NumberField({
			allowBlank : true,
			maxLength : 100
		})},
    {id: 'score',header: "考核得分", width: 100, sortable: true, dataIndex: 'score'},
	{
	  header : "操作",
	  width : 150,
	  dataIndex : 'operation',
	  align : 'center',
	  renderer : function(value, cellmeta, record, rowIndex, columnIndex, store) {
			var contentId = Ext.id();
			var btn = createGridButton.defer(1, this, [contentId]);
			function createGridButton() {
				return new Ext.Button({
							text : '填报',
							iconCls : 'icon-edit',
							height: 24,
							handler : function() {
								grid_orgcheckself.getSelectionModel().selectRow(rowIndex);
								window_write_orgcheck.show(this);
								window_write_orgcheck.center(); 
								var frm = Ext.getCmp("form_write_orgcheck").getForm();
								// set value
								frm.findField('orgCheck.id').setValue(record.get("id"));
								frm.findField('orgCheck.detailId').setValue(record.get("detailId"));
								frm.findField('orgCheck.orgId').setValue(record.get("orgId"));
								frm.findField('orgCheck.goStatus').setValue(record.get("goStatus"));
								frm.findField('orgCheck.finishResult').setValue(record.get("finishResult"));
								frm.findField('orgCheck.point').setValue(record.get("point"));
								frm.findField('orgCheck.score').setValue(record.get("score"));
								frm.findField('orgCheck.selfScore').setValue(record.get("selfScore"));
								frm.findField('orgCheck.memo').setValue(record.get("memo"));
								frm.findField('orgCheck.checkOrgId').setValue(record.get("checkOrgId"));
								frm.findField('orgCheck.checkResult').setValue(record.get("checkResult"));
								frm.findField('orgCheck.year').setValue(record.get("year"));
								frm.findField('orgCheck.type').setValue(record.get("type"));
								frm.clearInvalid();
							} 
						}).render(document.body, contentId);
			}
		return ('<div id="' + contentId + '"/>');
//	  renderer : function(value, cellmeta, record, rowIndex, columnIndex,store) {
//		return " <button type='button' onclick='writeOrgCheckSelf("+ record + ")'>填报 </button>";
	}}
]);

function writeOrgCheckSelf(record){
	alert(record);
//	window_write_orgcheck.show(this);
//	window_write_orgcheck.center(); 
//	var frm = Ext.getCmp("form_wriet_orgcheck").getForm();
//	// set value
//	frm.findField('orgCheck.id').setValue(record.get("id"));
//	frm.findField('orgCheck.detailId').setValue(record.get("detailId"));
//	frm.findField('orgCheck.orgId').setValue(record.get("orgId"));
//	frm.findField('orgCheck.goStatus').setValue(record.get("goStatus"));
//	frm.findField('orgCheck.finishResult').setValue(record.get("finishResult"));
//	frm.findField('orgCheck.score').setValue(record.get("score"));
//	frm.findField('orgCheck.selfScore').setValue(record.get("selfScore"));
//	frm.findField('orgCheck.memo').setValue(record.get("memo"));
//	frm.findField('orgCheck.checkOrgId').setValue(record.get("checkOrgId"));
//	frm.findField('orgCheck.checkResult').setValue(record.get("checkResult"));
//	frm.findField('orgCheck.year').setValue(record.get("year"));
//	frm.findField('orgCheck.type').setValue(record.get("type"));
}
var ds_orgcheckself = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/orgcheck/findOrgCheckSelf.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		totalProperty : 'totalProperty'
	}, [{
			name : 'id',
			type : 'int'
		}, {
			name : 'detailId',
			type : 'int'
		}, {
			name : 'content',
			type : 'string'
		}, {
			name : 'orgId',
			type : 'int'
		}, {
			name : 'goStatus',
			type : 'string'
		}, {
			name : 'finishResult',
			type : 'string'
		}, {
			name : 'point',
			type : 'float'
		}, {
			name : 'score',
			type : 'float'
		}, {
			name : 'selfScore',
			type : 'float'
		}, {
			name : 'checkOrgId',
			type : 'int'
		}, {
			name : 'memo',
			type : 'string'
		}, {
			name : 'checkOrgId',
			type : 'int'
		}, {
			name : 'checkResult',
			type : 'string'
		}, {
			name : 'year',
			type : 'int'
		}, {
			name : 'type',
			type : 'string'
		}, {
			name : 'checkStatus',
			type : 'string'
		}])
});
var grid_orgcheckself = new Ext.grid.EditorGridPanel({
//			title : '请填报（自评分,进展状态,完成情况）',
//			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit : true
			},
			region : 'center',
			cm : cm_orgcheckself,
			sm : new Ext.grid.RowSelectionModel({
						singleSelect : true
					}),
			store : ds_orgcheckself,
			autoExpandColumn : 'goStatus',
//			tbar : [btn_add_orgcheck, '-', btn_edit_orgcheck, '-', btn_del_orgcheck, '-', btn_search_orgcheck],
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_orgcheckself,
						displayInfo : false,
						displayMsg : '第 {0} - {1} 条  共 {2} 条',
						refreshText : "刷新",
						emptyMsg : "没有记录"
					}),
			// 检测至少选择一个
			checkOne : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请选择一条的记录！");
					return false;
				} else if (selections.length != 1) {
					Ext.MessageBox.alert("提示", "不能选择多行！");
					return false;
				}
				return true;
			},

			// 检测必须选择一个
			checkMany : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
					return false;
				}
				return true;
			},
			listeners : {
				'afteredit' : function(e) {
					Ext.Ajax.request({
						url : '/supervise/biz/orgcheck/updateOrgCheck.action',
						params : {
							fieldName : e.field,
							fieldValue : e.value,
							id : e.record.data.id
						},
						success : function() {
							// alert("数据修改成功！");
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '修改数据发生错误,操作将被回滚!',
								fn : function() {
									e.record.set(e.field, e.originalValue);
								},
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}

		});
var qpanel_orgcheckself = new Ext.Panel({
//	title : '<h3 style="color:Green">请输入查询条件</h3>',
	height : 100,
	region : "north",
	baseCls:"x-plain",
	style:"padding:5px",
	layout:"column",
	items:[{
		columnWidth:0.6,
		layout:"form",
		frame : true,
		baseCls : 'x-plain',
		items : [new Ext.FormPanel({
			id: 'form_query_orgcheckself',
			labelAlign : 'right',
			border : false,
			baseCls : 'x-plain',
			bodyStyle : 'padding:5px 5px 0',
			defaults : {
				anchor : '90%',
				width : 240,
				msgTarget : 'side' // 验证信息显示右边
			},
			defaultType : 'textfield',
			items:[{
					xtype : 'combo',
					fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
					hiddenName : 'year',
					valueField : 'value',
					displayField : 'text',
					value: check_year,
					mode : 'local',
					store : ds_year_select,
					value : select_year,
					selectOnFocus : true,
					editable : false,
					allowBlank : false,
					typeAhead : true,
					readOnly : true,
					triggerAction : 'all',
					loadingText : '加载中...',
					emptyText : '请选择考核年度'
				}, {
					fieldLabel : '考核内容',
					name : 'content'
				}],
			buttonAlign : 'center',
			minButtonWidth : 60,
			buttons : [{
						text : '查询',
						handler : function(btn) {
							var frm = btn.ownerCt.ownerCt.getForm();
							ds_orgcheckself.load({params : {
								start : 0,limit : 20,
								year : frm.findField('year').getValue(),
								content : frm.findField('content').getValue()
							}});}
					},{
						text : '重置',
						handler : function() { 
							this.ownerCt.ownerCt.getForm().reset();
						}
					}]
		})]
	}]
});

var query_orgcheckself =  new Ext.Panel({
	frame : false,
	region : 'center',
	labelWidth : 65,
	title : '考核指标填报',
	labelPad : 45,
	buttonAlign : 'center',
	layout : 'border',
	items : [qpanel_orgcheckself, grid_orgcheckself]
});	
var p_orgcheckself = {
	id : 'orgcheckself-panel',
	border : false,
	layout : 'border',
	items : [query_orgcheckself]
};
  
